package Protocol
{
	import flash.utils.ByteArray;
	import KK.Data.ProtocolData;


	public class Apple extends ProtocolData
	{
		private var test:CTest;
		private var testArray:Vector.<CTest>;
		private var friendsName:Vector.<String>;
		private var friendId:Vector.<int>;

		public function Serialize(bytes:ByteArray):void
		{
			var i:int = 0;
			var len:int = 0;

			test.Serialize(bytes);
			bytes.writeShort(testArray.length)
			i = 0;
			len = testArray.length;
			while (i < len) {
				testArray[i].Serialize(bytes);
				i++;
			}

			bytes.writeShort(friendsName.length)
			i = 0;
			len = friendsName.length;
			while (i < len) {
				bytes.writeShort(friendsName[i].length);
				bytes.writeMultiByte(friendsName[i],"utf-8");
				i++;
			}

			bytes.writeShort(friendId.length)
			i = 0;
			len = friendId.length;
			while (i < len) {
				bytes.writeInt(friendId[i]);
				i++;
			}

		}


		public function Unserialize(bytes:ByteArray):void
		{
			var len:int = 0;
			var i:int = 0;

			test.Unserialize(bytes);
			len = bytes.readShort();
			testArray = new Vector.<CTest>(len, true);

			i = 0;
			while (i < len) {
				testArray[i].Unserialize(bytes);
				i++;
			}

			len = bytes.readShort();
			friendsName = new Vector.<String>(len, true);

			i = 0;
			while (i < len) {
				var length = bytes.readShort();

				friendsName[i] = bytes.readMultiByte(length, "utf-8");
				i++;
			}

			len = bytes.readShort();
			friendId = new Vector.<int>(len, true);

			i = 0;
			while (i < len) {
				friendId[i] = bytes.readInt();
				i++
			}

		}


		public function getSize():uint
		{
			var result:uint = 0;
			var i:int = 0;
			var len:int = 0;

			result += test.getSize();
			result += INT16;

			i = 0;
			len = testArray.length;
			while (i < len) {
				result += testArray[i].getSize();
				i++
			}

			result += INT16;

			i = 0;
			len = friendsName.length;
			while (i < len) {
				result += INT16 + friendsName[i].length;
				i++;
			}

			result += INT32 * friendId.length;
			return result;
		}

	};
}

